package com.bangdream.michelia.model;

import com.alibaba.fastjson.JSON;
import com.bangdream.michelia.application.AppCurrentUser;
import com.bangdream.michelia.contract.UserContract;
import com.bangdream.michelia.tool.retrofithttp.RetroFactory;
import com.bangdream.michelia.utils.L;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @date on 2018/7/24 16:20
 * @email 909732184@qq.com
 */
public class IUserModelImpl extends BaseModel implements UserContract.IUserModel {
    //获取验证码
    @Override
    public Observable getRegisterCode(UserContract.CodeType type, String stPhoneNumber) {
        //注册获取验证码
        if(type==UserContract.CodeType.REGISTER) {
            return RetroFactory.getInstance().getRegisterCode(stPhoneNumber);
        }
        //登录失败三次需要验证 获取验证码
        else if (type==UserContract.CodeType.LOGINPHONE){
            return RetroFactory.getInstance().getLoginPhoneCode(stPhoneNumber);
        }
        //通过验证码登录 获取验证码
        else if(type==UserContract.CodeType.LOGINCODE){
            return RetroFactory.getInstance().getCodeLoginCode(stPhoneNumber);
        }
        //找回密码获取验证码
        else if(type==UserContract.CodeType.FINDPASSWORD){
            return RetroFactory.getInstance().getFindPasswordCode(stPhoneNumber);
        }else {
            return RetroFactory.getInstance().getRegisterCode(stPhoneNumber);
        }
    }
    //注册
    @Override
    public Observable actionRegister(String stCode, String stPhone, String stPassword) {
        Map<String,String> map = new HashMap<>();
        map.put("code",stCode);
        map.put("loginType","1");
        map.put("phone",stPhone);
        map.put("password",stPassword);
        String jsonString = JSON.toJSONString(map);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().actionRegister(requestBody);
    }

    //登录
    @Override
    public Observable actionLogin(String accountName, String password,String verify) {
        Map<String,String> map = new HashMap<>();
        map.put("accountName",accountName);
        map.put("loginType","1");
        map.put("password",password);
        //1-用户名登录；2-手机号登录；3邮箱登录
        map.put("type","2");
        if(verify!=null){
            //验证码
            map.put("verify",verify);
        }
        String jsonString = JSON.toJSONString(map);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().actionLogin(requestBody);
    }

    @Override
    public Observable actionCodeLogin(String phone, String verify) {
        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("loginType","1");
        map.put("verify",verify);
        String jsonString = JSON.toJSONString(map);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().actionCodeLogin(requestBody);
    }
    //第三方登录
    @Override
    public Observable actionThirdLogin(String faceUrl, String nickName, String openId, String userType) {
        Map<String,String> map = new HashMap<>();
        map.put("loginType", "1");
        if(faceUrl!=null) {
            map.put("faceUrl", faceUrl);
        }
        if(nickName!=null) {
            map.put("nickName", nickName);
        }
        if(openId!=null) {
            map.put("openId", openId);
        }
        if(openId!=null) {
            map.put("userType", userType);
        }
        String jsonString = JSON.toJSONString(map);
        L.d(jsonString);
        //RequestBody requestBody = RequestBody.create(MediaType.parse("text/html; charset=utf-8"),
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().actionThirdLogin(requestBody);
    }

    @Override
    public Observable actionFindPassword(String phone, String verify,String stPassword) {
//        @Query("mobile") String mobile,@Query("verify") String verify
        Map<String,String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("password", stPassword);
        map.put("code", verify);
        String jsonString = JSON.toJSONString(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().actionFindPassword(requestBody);
    }

    @Override
    public Observable getUserMsg(String id, String token) {
        return RetroFactory.getInstance().getUserInfo(id,token);
    }

    @Override
    public Observable getSTS(String token) {
        return RetroFactory.getInstance().getSTS(token);
    }

    @Override
    public Observable uploadPic(String token, String urlPath) {
        Map<String,String> map = new HashMap<>();
        map.put("path",urlPath);
        map.put("userId", AppCurrentUser.getInstance().getUserInfo().getId());
        String jsonString = JSON.toJSONString(map);
        L.d(jsonString);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().uploadPic(token,requestBody);

    }

    @Override
    public Observable editUser(Map map) {
        String jsonString = JSON.toJSONString(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        return RetroFactory.getInstance().eidtUser(AppCurrentUser.getInstance().getUserInfo().getId(),
                AppCurrentUser.getInstance().getToken(),requestBody);

    }


    @Override
    public Observable getFinishstate() {
        Map<String,String> map=new HashMap<>();
//        map.put("userId",AppCurrentUser.getInstance().getUserInfo().getId());
        String jsonString =JSON.toJSONString(map);
        L.d(jsonString);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),
                jsonString);
        String stTokent = AppCurrentUser.getInstance().getToken();
        return RetroFactory.getInstance().getFinishstate(stTokent,requestBody);
    }

}
